from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep

# chromedriver 版本为 137
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)


def login(username: str, password: str):
    browser.get("http://146.56.217.9:9131")
    wait.until(EC.presence_of_element_located((By.ID, "username"))).clear()
    browser.find_element(By.ID, "username").send_keys(username)
    browser.find_element(By.ID, "password").clear()
    browser.find_element(By.ID, "password").send_keys(password)
    browser.find_element(By.CLASS_NAME, "button").click()


if __name__ == '__main__':
    try:
        # 多组测试账号
        login_data = [
            {"username": "admin", "password": "admin"},#负面
            {"username": "1234567890asdfghjk", "password": "adimin"},#账号名过长的负面
            {"username": "admin_9131", "password": "1251xasxiashdiashdiasdhasiudhasiudhasdasdasdasd"},#密码过长的负面测试
            {"username": "' OR 1=1 --", "password": "admin"},#SQL注入测试
            {"username": "admin_9131", "password": "admin"}#正常登入
        ]

        for creds in login_data:
            try:
                login(creds["username"], creds["password"])
                print(f"尝试登录：{creds['username']}")
                # 可选：判断是否登录成功，比如等待某个元素出现
                sleep(2)
            except Exception as e:
                print(f"登录失败：{creds['username']} - {type(e).__name__} {str(e)}")
    except Exception as e:
        print("主流程异常：", type(e).__name__, str(e))
    finally:
        sleep(1)
        browser.quit()
